/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;
import mx.itschapala.dsw2.bl.EventosBLLocal;
import mx.itschapala.dsw2.bl.RegisterBLLocal;
import mx.itschapala.dsw2.modelo.Eventos;
import mx.itschapala.dsw2.modelo.Register;

/**
 *
 * @author User
 */
@ManagedBean
@SessionScoped
public class EventoBean {
    @EJB
    private RegisterBLLocal registerBL;
    @EJB
    private EventosBLLocal evntosBL;
    
    
    
    List<Eventos> lista;
    List<Register> listRe;
    private Eventos evn;
    private String accion;
    private int n;
    private Register regis;
    private int idUsuarioSeleccionado;
    /**
     * Creates a new instance of EventoBean
     */
    
    
    public EventoBean() {
        evn=new Eventos();
    }

    public List<Eventos> getLista() {
        lista= evntosBL.getList();
        return lista;
    }

    public Eventos getEvn() {
        return evn;
    }

    public void setEvn(Eventos evn) {
        this.evn = evn;
    }

    public List<Register> getListRe() {
        listRe=registerBL.getList();
        return listRe;
    }

    public Register getRegis() {
        return regis;
    }

    public void setRegis(Register regis) {
        this.regis = regis;
    }

    public int getIdUsuarioSeleccionado() {
        return idUsuarioSeleccionado;
    }

    public void setIdUsuarioSeleccionado(int idUsuarioSeleccionado) {
        this.idUsuarioSeleccionado = idUsuarioSeleccionado;
    }
    
    
    
    
    //-----------------Acciones------------------------------
    public String procesarAccion(){
        if(accion.equalsIgnoreCase("Nuevo")){
       evntosBL.register(evn);
        }
        else
        if(accion.equalsIgnoreCase("Editar")){
           evntosBL.modify(evn);
        }
        else if(accion.equalsIgnoreCase("Eliminar")){
            evntosBL.delete(evn);
         
         
        }
        accion="";
    return "Evento";
    }
    
    
   public String limpiarAccion(){
   evn=new Eventos();
   return "EvevtoCrearEditar";
    }
//-----------------Listener------------------------------

public void prepararNuevo(ActionEvent evt){
evn=new Eventos();
accion="Nuevo";
}
public void prepararEditar(ActionEvent evt)   
{
System.out.println(evt.getComponent().getClientId());
 n=Integer.parseInt(evt.getComponent().getClientId().split(":")[2]);
    System.out.println("el evento fue:"+n);
    evn=lista.get(n);
    accion="Editar";
}     


public void prepararEliminar(ActionEvent evt)   
{
    int x;
System.out.println(evt.getComponent().getClientId());
 n=Integer.parseInt(evt.getComponent().getClientId().split(":")[2]);
    System.out.println("el evento es:"+n);
    evn=lista.get(n);
   
    
    accion="Eliminar";
}    

public void prepararEvento(){
    evn=new Eventos();
    evn.setRegisteridRegister(registerBL.findById(idUsuarioSeleccionado));
    
    System.out.println(evn.getDate());
    System.out.println(evn.getEvent());
    System.out.println(evn.getLocation());
    System.out.println(evn.getTime());
    System.out.println(evn.getRegisteridRegister());
accion="Nuevo";
}

    
    
    
}
